package com.da.javatest.leetCode;

/**
 * @author chenlida
 * @date 2019/1/4 17:07
 * @description
 */
public class Code463 {
    public int islandPerimeter(int[][] grid) {
        int total = 0;
        if (grid == null) {
            return total;
        }
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                int target = grid[i][j];
                if (target == 0) { continue; }
                try {
                    if (grid[i][j - 1] == 0) {total += 1;}
                } catch (Exception e) {
                    total += 1;
                }
                try {
                    if (grid[i][j + 1] == 0) {total += 1;}
                } catch (Exception e) {
                    total += 1;
                }
                try {
                    if (grid[i + 1][j] == 0) {total += 1;}
                } catch (Exception e) {
                    total += 1;
                }
                try {
                    if (grid[i - 1][j] == 0) {total += 1;}
                } catch (Exception e) {
                    total += 1;
                }

            }
        }
        return total;
    }
}
